import socket
import fcntl
import struct
import os
import commands
def get_Mask():
    (status,output)=commands.getstatusoutput('ifconfig | head -n 2| tail -n 1 | awk -F \':\' \'{print $4}\'')    
    return output
def get_ip_address(ifname='eth0'):
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  
        return socket.inet_ntoa(fcntl.ioctl(
            s.fileno(),  
            0x8915, # SIOCGIFADDR  
            struct.pack('256s', ifname[:15])  
        )[20:24]) 
    except:
        ips = os.popen("LANG=C ifconfig | grep \"inet addr\" | grep -v \"127.0.0.1\" | awk -F \":\" '{print $2}' | awk '{print $1}'").readlines()
        if len(ips) > 0:
            return ips[0]
    return ''


